package com.wang.wheeldemo;

import android.os.Bundle;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import kankan.wheel.widget.lintener.OnButtonClickListener;
import kankan.wheel.widget.model.AllLocationsMode;
import kankan.wheel.widget.model.DataModel;
import kankan.wheel.widget.provinceCityArea.IOSCharacterPickerView;


public class MainActivity extends AppCompatActivity {

    private JSONObject mJsonObj;

    private static final int UPDATA_TEXT = 1;
    private List<DataModel> mProvinceDatas;

    private SparseArrayCompat<List<DataModel>> mCitisDatasMap = new SparseArrayCompat<>();

    private SparseArrayCompat<List<DataModel>> mAreaDatasMap = new SparseArrayCompat<>();

    List<AllLocationsMode> responses = new LinkedList<>();

    private final Object lock = new Object();


//    private CharacterPickerView view;

    private IOSCharacterPickerView mIOS;

    //    private WheelView mWheel;
//
//    private WheelView2 mWheel2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIOS = (IOSCharacterPickerView) findViewById(R.id.test2);
        getJson();
//        initDatas();
//        mIOS.setDefault(2, 202, 2202);
//        mIOS.setProvince(mProvinceDatas);
//        mIOS.setCity(mCitisDatasMap);
//        mIOS.setArea(mAreaDatasMap);
        mIOS.setButtonClickListener(new OnButtonClickListener() {
            @Override
            public void onClick(DataModel province, DataModel city, DataModel area) {
                Log.d("MainActivity", "mIOS.getY():" + mIOS.getBottom());
//                mIOS.setBottom(1000);
                Log.d("test", province.Name + "  " + city.Name + "  " + area.Name);
            }

            @Override
            public void onCancel() {
                Log.d("test", "cancel");

            }
        });

    }

    private void getJson() {
        RequestParams requestParams = new RequestParams("http://api.danjiguanjia.com/v2/Sales/linkageAddress/");
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
//                    JSONObject jsonobject = new JSONObject(result);
//                    mJsonObj = jsonobject;
                    JSONObject json = new JSONObject("{\"retcode\":\"2000\",\"msg\":\"success\",\"data\":[{\"n\":\"\\u6cb3\\u5317\",\"s\":[{\"n\":\"\\u77f3\\u5bb6\\u5e84\\u5e02\"},{\"n\":\"\\u79e6\\u7687\\u5c9b\\u5e02\"},{\"n\":\"\\u4fdd\\u5b9a\\u5e02\"},{\"n\":\"\\u627f\\u5fb7\\u5e02\"},{\"n\":\"\\u5510\\u5c71\\u5e02\"}]},{\"n\":\"\\u6cb3\\u5357\",\"s\":[{\"n\":\"\\u90d1\\u5dde\\u5e02\"},{\"n\":\"\\u5b89\\u9633\\u5e02\"},{\"n\":\"\\u9e64\\u58c1\\u5e02\"},{\"n\":\"\\u8bb8\\u660c\\u5e02\"},{\"n\":\"\\u6f2f\\u6cb3\\u5e02\"}]},{\"n\":\"\\u5c71\\u897f\",\"s\":[{\"n\":\"\\u592a\\u539f\\u5e02\"},{\"n\":\"\\u957f\\u6cbb\\u5e02\"},{\"n\":\"\\u664b\\u57ce\\u5e02\"},{\"n\":\"\\u8fd0\\u57ce\\u5e02\"},{\"n\":\"\\u4e34\\u6c7e\\u5e02\"}]},{\"n\":\"\\u5c71\\u4e1c\",\"s\":[{\"n\":\"\\u9752\\u5c9b\\u5e02\"},{\"n\":\"\\u6f4d\\u574a\\u5e02\"},{\"n\":\"\\u65e5\\u7167\\u5e02\"},{\"n\":\"\\u4e34\\u6c82\\u5e02\"},{\"n\":\"\\u804a\\u57ce\\u5e02\"}]},{\"n\":\"\\u9655\\u897f\",\"s\":[{\"n\":\"\\u897f\\u5b89\\u5e02\"},{\"n\":\"\\u5b9d\\u9e21\\u5e02\"},{\"n\":\"\\u54b8\\u9633\\u5e02\"},{\"n\":\"\\u6e2d\\u5357\\u5e02\"},{\"n\":\"\\u5ef6\\u5b89\\u5e02\"}]},{\"n\":\"\\u6e56\\u5317\",\"s\":[{\"n\":\"\\u6b66\\u6c49\\u5e02\"},{\"n\":\"\\u5b5d\\u611f\\u5e02\"},{\"n\":\"\\u8346\\u5dde\\u5e02\"},{\"n\":\"\\u9ec4\\u5188\\u5e02\"},{\"n\":\"\\u8944\\u9633\\u5e02\"}]},{\"n\":\"\\u6c5f\\u82cf\",\"s\":[{\"n\":\"\\u5357\\u4eac\\u5e02\"},{\"n\":\"\\u65e0\\u9521\\u5e02\"},{\"n\":\"\\u82cf\\u5dde\\u5e02\"},{\"n\":\"\\u6dee\\u5b89\\u5e02\"},{\"n\":\"\\u76d0\\u57ce\\u5e02\"}]},{\"n\":\"\\u9ed1\\u9f99\\u6c5f\",\"s\":[{\"n\":\"\\u54c8\\u5c14\\u6ee8\\u5e02\"},{\"n\":\"\\u9e21\\u897f\\u5e02\"},{\"n\":\"\\u4f73\\u6728\\u65af\\u5e02\"},{\"n\":\"\\u4e03\\u53f0\\u6cb3\\u5e02\"},{\"n\":\"\\u7261\\u4e39\\u6c5f\\u5e02\"}]}]}");
                    mJsonObj = json;
                    Timer timer = new Timer();
                    TimerTask task = new TimerTask() {
                        @Override
                        public void run() {
                    initDatas();
                            mIOS.setDefault(2, 202, 2202);
                            mIOS.setProvince(mProvinceDatas);
                            mIOS.setCity(mCitisDatasMap);
                            mIOS.setArea(mAreaDatasMap);
                        }
                    };
                    timer.schedule(task, 500);



                    Log.d("MainActivity","onSuccess: "+result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }

    private void initDatas() {
        try {
//            JSONObject json = new JSONObject("{\"retcode\":\"2000\",\"msg\":\"success\",\"data\":[{\"n\":\"\\u6cb3\\u5317\",\"s\":[{\"n\":\"\\u77f3\\u5bb6\\u5e84\\u5e02\"},{\"n\":\"\\u79e6\\u7687\\u5c9b\\u5e02\"},{\"n\":\"\\u4fdd\\u5b9a\\u5e02\"},{\"n\":\"\\u627f\\u5fb7\\u5e02\"},{\"n\":\"\\u5510\\u5c71\\u5e02\"}]},{\"n\":\"\\u6cb3\\u5357\",\"s\":[{\"n\":\"\\u90d1\\u5dde\\u5e02\"},{\"n\":\"\\u5b89\\u9633\\u5e02\"},{\"n\":\"\\u9e64\\u58c1\\u5e02\"},{\"n\":\"\\u8bb8\\u660c\\u5e02\"},{\"n\":\"\\u6f2f\\u6cb3\\u5e02\"}]},{\"n\":\"\\u5c71\\u897f\",\"s\":[{\"n\":\"\\u592a\\u539f\\u5e02\"},{\"n\":\"\\u957f\\u6cbb\\u5e02\"},{\"n\":\"\\u664b\\u57ce\\u5e02\"},{\"n\":\"\\u8fd0\\u57ce\\u5e02\"},{\"n\":\"\\u4e34\\u6c7e\\u5e02\"}]},{\"n\":\"\\u5c71\\u4e1c\",\"s\":[{\"n\":\"\\u9752\\u5c9b\\u5e02\"},{\"n\":\"\\u6f4d\\u574a\\u5e02\"},{\"n\":\"\\u65e5\\u7167\\u5e02\"},{\"n\":\"\\u4e34\\u6c82\\u5e02\"},{\"n\":\"\\u804a\\u57ce\\u5e02\"}]},{\"n\":\"\\u9655\\u897f\",\"s\":[{\"n\":\"\\u897f\\u5b89\\u5e02\"},{\"n\":\"\\u5b9d\\u9e21\\u5e02\"},{\"n\":\"\\u54b8\\u9633\\u5e02\"},{\"n\":\"\\u6e2d\\u5357\\u5e02\"},{\"n\":\"\\u5ef6\\u5b89\\u5e02\"}]},{\"n\":\"\\u6e56\\u5317\",\"s\":[{\"n\":\"\\u6b66\\u6c49\\u5e02\"},{\"n\":\"\\u5b5d\\u611f\\u5e02\"},{\"n\":\"\\u8346\\u5dde\\u5e02\"},{\"n\":\"\\u9ec4\\u5188\\u5e02\"},{\"n\":\"\\u8944\\u9633\\u5e02\"}]},{\"n\":\"\\u6c5f\\u82cf\",\"s\":[{\"n\":\"\\u5357\\u4eac\\u5e02\"},{\"n\":\"\\u65e0\\u9521\\u5e02\"},{\"n\":\"\\u82cf\\u5dde\\u5e02\"},{\"n\":\"\\u6dee\\u5b89\\u5e02\"},{\"n\":\"\\u76d0\\u57ce\\u5e02\"}]},{\"n\":\"\\u9ed1\\u9f99\\u6c5f\",\"s\":[{\"n\":\"\\u54c8\\u5c14\\u6ee8\\u5e02\"},{\"n\":\"\\u9e21\\u897f\\u5e02\"},{\"n\":\"\\u4f73\\u6728\\u65af\\u5e02\"},{\"n\":\"\\u4e03\\u53f0\\u6cb3\\u5e02\"},{\"n\":\"\\u7261\\u4e39\\u6c5f\\u5e02\"}]}]}");
//            mJsonObj = json;
            JSONArray jsonArray = mJsonObj.getJSONArray("data");
            mProvinceDatas = new LinkedList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonP = jsonArray.getJSONObject(i);
                String province = jsonP.getString("n");

                mProvinceDatas.add(new DataModel(i, province));

                JSONArray jsonCs = null;
                try {
                    /**
                     * Throws JSONException if the mapping doesn't exist or is
                     * not a JSONArray.
                     */
                    jsonCs = jsonP.getJSONArray("s");
                } catch (Exception e1) {
                    continue;
                }
                List<DataModel> mCitiesDatas = new LinkedList<>();
                List<String> list = new ArrayList<>();
                for (int j = 0; j < jsonCs.length(); j++) {
                    JSONObject jsonCity = jsonCs.getJSONObject(j);
                    String city = jsonCity.getString("n");// ������
                    mCitiesDatas.add(new DataModel(i * 100 + j, city));
                    list.add(city);
//                    JSONArray jsonAreas = null;
//                    try {
//                        /**
//                         * Throws JSONException if the mapping doesn't exist or
//                         * is not a JSONArray.
//                         */
//                        jsonAreas = jsonCity.getJSONArray("a");
//                    } catch (Exception e) {
//                        continue;
//                    }
//
//                    List<DataModel> mAreasDatas = new LinkedList<>();// ��ǰ�е�������
//                    for (int k = 0; k < jsonAreas.length(); k++) {
//                        String area = jsonAreas.getJSONObject(k).getString("s");// ���������
//                        mAreasDatas.add(new DataModel(i * 1000 + j * 100 + k, area));
//                    }
//                    mAreaDatasMap.put(i * 100 + j, mAreasDatas);

                }
                mCitisDatasMap.put(i, mCitiesDatas);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        mJsonObj = null;

    }


//    private void initJsonData() {
//        try {
//            StringBuffer sb = new StringBuffer();
//            InputStream is = getAssets().open("city.json");
//            int len = -1;
//            byte[] buf = new byte[1024];
//            while ((len = is.read(buf)) != -1) {
//                sb.append(new String(buf, 0, len, "gbk"));
//            }
//            is.close();
//            mJsonObj = new JSONObject(sb.toString());
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//    }


}
